<?php

require_once 'ApiException.php';
require_once 'application/libraries/ApiClient.php';

class ChartboostApiClient extends ApiClient
{
	const CLIENT_VERSION = 'Chartboost-PHP/v2.20100304';
	const CURL_TIMEOUT = 10;
	const OBJECT_SITE = 'apps';
	const DIMENSION_DAY = 'daily';
	const DIMENSION_WEEK = 'weekly';
	const DIMENSION_MONTH = 'monthly';


	/**
	 * @param string $client_key
	 * @param int $timeout The maximum number of seconds to allow cURL functions to execute.
	 * @param boolean $retry_rate_limited_requests True to have the client retry requests that were rate limited (recommended).
	 */
	public function __construct($client_key,$token, $timeout = self::CURL_TIMEOUT, $retry_rate_limited_requests = true)
	{
		$this->client_key = $client_key;
		$this->token = $token;
		$this->timeout = $timeout;
		$this->retry_rate_limited_requests = $retry_rate_limited_requests;
	}



	/**
	 * This function will return ALL pages of data for the associated API request.
	 * This function will retry a request that was rate limited by the API.
	 * This function will throw and AdMobApiException if an unrecoverable error occurs.
	 * @param string $object
	 * @param string $method
	 * @param array $params
	 * @param boolean $post
	 * @param boolean $https
	 */
	public function getData($object, $method, $params, $post, $https = false)
	{
		$response = $this->getResponse($object, $method, $params, $post, $https);
		return $response;
	}

	protected function getUrl($object, $method, $https)
	{
		$protocol = $https ? 'https' : 'http';
		$url = null;
		if ($method) {
			$url = "$protocol://api.chartboost.com/$method/$object/";
		} else {
			$url = "$protocol://api.chartboost.com/$object/";
		}
		return $url;
	}


	protected function getResponse($object, $method, $params, $post, $https)
	{
		try {

			$this->clearParams($params);

			$params['user_id'] = $this->client_key;

			if (isset($this->token)) {
				$params['user_signature'] = $this->token;
			}

			$url = $this->getUrl($object, $method, $https);
			$data = http_build_query($params);

			$curl = curl_init();
			if ($post) {
				curl_setopt($curl, CURLOPT_POST, true);
				curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
			} else {
				$url = $url . "?" . $data;
			}


			curl_setopt($curl, CURLOPT_URL, $url);
			curl_setopt($curl, CURLOPT_PORT, $this->getPort($https));
			curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
			curl_setopt($curl, CURLOPT_USERAGENT, self::CLIENT_VERSION);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

			$json = curl_exec($curl);
			$errno = curl_errno($curl);
			$stored_ex = null;
		} catch (Exception $ex) {
			$stored_ex = $ex;
		}

		curl_close($curl);

		if ($errno !== 0) {
			$e = new ApiException("Chartboost API - Curl error $errno");
			$e->setApiUrl($url);
			$e->setApiParameters($params);
			throw $e;
		}

		$response = json_decode($json, true);
		if (empty($response)) {
			$e = new ApiException("Unable to json decode Chartboost API response [RESPONSE $json]");
			$e->setApiUrl($url);
			$e->setApiParameters($params);
			throw $e;
		} else if (!empty($response["status"])) {
			$e = new ApiException("status: " . $response["status"] . " message: " . $response["message"]);
			//$e->setApiUrl($url);
			//$e->setApiParameters($params);
			throw $e;
		}
		return $response;
	}

}
